const mongoose = require("mongoose");
const { Schema } = mongoose;

const couponSchema = new Schema({
    discountType: {
        type: String,
        required: true,
        default: 'fixed',
        enum: ['percentage', 'fixed']
    },
    name: {
        type: String,
        required: true,
    },
    discountValue: {
        type: Number,
        required: true,
        default: 0,
    },
    minPurchase: {
        type: Number,
        default: 0,
    },

    isActive: {
        type: Number,
        enum: [1, 2, 3], // 1: 未使用，2: 已使用，3: 已过期
        default: 1
    },
    limit: { // 限制使用次数
        type: Number,
        default: 1, // 默认限制为1次
    },
    applicableCategories: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "category", // 关联到分类表的ID字段
        }
    ],
    applicableProducts: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "good", // 关联到商品表的ID字段
        }
    ],
    startDate: {
        type: Number,
        default: Date.now
    },
    endDate: {
        type: Number,
        default: Date.now
    },
});
const coupon = mongoose.model("coupon", couponSchema);
module.exports = coupon